<?php
namespace app\common\behavior;

use think\Db;

class Home
{
    /**
     * CMS模型点击量
     * @param $params
     */
    public function cmsClick(&$params)
    {
        $request = request();
        $mid = $request->param('mid/d',1);
        $aid = $request->param('aid/d',0);
        cms_moudle($mid)->where('aid',$aid)->setInc('click',1,10);  //延迟10S更新
    }
    /**
     * 商品模型点击量
     * @param $params
     */
    public function goodsItem(&$params)
    {
        $goodsId = $params['goods_id'];
        $uid = $params['uid'];
        //更新点击量
        Db::table('shop_goods')->where('goods_id', $params['goods_id'])->setInc('click',1,10); //延迟10S更新
        if($uid>0){ //加入浏览历史
            $historyInfo = Db::table('shop_goods_history')->where('uid',$uid)->where('goods_id',$goodsId)->find();
            if($historyInfo){
                Db::table('shop_goods_history')->where('id',$historyInfo['id'])->setField('create_time',time());
            }else{
                $count = Db::table('shop_goods_history')->where('uid',$uid)->count();
                if($count>59){
                    $historyInfo = Db::table('shop_goods_history')->where('uid',$uid)->order('create_time asc')->find();
                    $historyData = ['goods_id'=>$goodsId,'uid'=>$uid,'create_time'=>time()];
                    Db::table('shop_goods_history')->where('id',$historyInfo['id'])->update($historyData);
                }else{
                    $historyData = ['goods_id'=>$goodsId,'uid'=>$uid,'create_time'=>time()];
                    Db::table('shop_goods_history')->insert($historyData);
                }
            }
        }
    }
}